<?php
/**
 * Created by PhpStorm.
 * User: CJJ
 * Date: 2017/8/24
 * Time: 11:04
 */



require_once ('../../SQLBase.php');


class  LeaderManager{
    /*
     * 获取全部部门基本信息
     */
    function getAllSectionData() { // 获取表内容
        $db = new SQLBase ();
        $queryStr = 'SELECT * FROM allsection WHERE section_state=1;';
        $result = $db->queryData ( $queryStr );
        if (! $result||0==$result->num_rows) {
            $result_array ['state'] = - 1; // -1表示数据库出错
        } else {
            $result_array ['state'] = 1;
            while ( !!$temp = $result->fetch_array () ) {
                $result_array ['allsection'][] = array (
                    section_ID => $temp ['section_ID'],
                    section_name => $temp ['section_name'],
                    section_type=>$temp ['section_type']
                );
            }
        }
        return $result_array;
    }
    /*
     * getPageCount 获取数据条数
     */
    function getPageCount(){
        $db=new SQLBase();
        $queryStr='SELECT count(*) FROM leader WHERE power_level=1;';
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $temp=$result->fetch_assoc();
            $resultArrayAll['level1PageCounts']=$temp['count(*)'];
            $queryStr='SELECT count(*) FROM leader WHERE power_level=2;';
            $result=$db->queryData($queryStr);
            if(!$result){
                $resultArrayAll['state']=-1;
            }else{
                $temp=$result->fetch_assoc();
                $resultArrayAll['level2PageCounts']=$temp['count(*)'];
                $queryStr='SELECT count(*) FROM leader WHERE power_level=3;';
                $result=$db->queryData($queryStr);
                if(!$result){
                    $resultArrayAll['state']=-1;
                }else{
                    $temp=$result->fetch_assoc();
                    $resultArrayAll['level3PageCounts']=$temp['count(*)'];
                    $resultArrayAll['state']=1;

                }
            }
        }
        return $resultArrayAll;
    }
    /*
     * 获取部门领导数据分页显示
     */
    function getLevel1Limit($n,$m){
        $db=new SQLBase();
        $queryStr='SELECT leader.user_id,`user`.user_name,allsection.section_name,leader.is_approve,leader.is_fee FROM user,allsection,leader WHERE `user`.user_ID=leader.user_id AND allsection.section_ID=leader.section_id AND leader.power_level=1 LIMIT '.$n.','.$m.';';
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $resultArrayAll['state']=1;
            while ($temp=$result->fetch_assoc()){
                $resultArrayAll['data'][]=$temp;
            }
        }
        return $resultArrayAll;
    }
    /*
     * editLevel1GetInfo 编辑时先加载个人数据
     */
    function editLevel1GetInfo($userId,$sectionName){
        $db=new SQLBase();
        $queryStr='SELECT section_ID,section_name FROM allsection WHERE section_state=1 AND section_ID NOT IN(SELECT section_id FROM leader  WHERE user_id='.$userId.' AND section_id NOT IN(SELECT section_ID FROM allsection WHERE section_name="'.$sectionName.'"));';
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            while ($temp=$result->fetch_assoc()){
                $resultArrayAll['allsection'][]=$temp;
            }
            $queryStr='SELECT leader.user_id,`user`.user_name,allsection.section_name,leader.is_approve,leader.is_fee FROM user,allsection,leader WHERE `user`.user_ID=leader.user_id AND allsection.section_ID=leader.section_id AND leader.power_level=1 AND leader.power_level=1 AND allsection.section_name="'.$sectionName.'" AND leader.user_id='.$userId.';';
            $result=$db->queryData($queryStr);
            if(!$result){
                $resultArrayAll['state']=-1;
            }else{
                $resultArrayAll['state']=1;
                $resultArrayAll['data'][]=$result->fetch_assoc();
            }
        }
        return $resultArrayAll;
    }
    /*
     * editLevel1UpdateInfo 编辑时编辑后更新数据
     */
    function editLevel1UpdateInfo($data) {
        $db = new SQLBase ();
        $queryStr = 'UPDATE leader SET section_id=' . $data ['sectionId'] . ',is_approve=' . $data ['isApprove'] . ',is_fee=' . $data ['isFee'] . ' WHERE user_id=' . $data ['userId'] . ' AND section_id=(SELECT section_ID FROM allsection WHERE section_name="'.$data['sectionName'].'");';
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $resultArrayAll['state']=1;
        }
        return $resultArrayAll;
    }
    /*
     * deleteLevel1Info 删除等级1某个领导
     */
    function deleteLevel1Info($userId,$sectionName) {
        $db = new SQLBase ();
        $queryStr = 'DELETE FROM leader WHERE power_level=1 AND user_id=' . $userId . ' AND section_id IN(SELECT section_ID FROM allsection WHERE section_name="'.$sectionName.'");';
        $result = $db->queryData ( $queryStr );
        if (! $result) {
            $resultArrayAll ['state'] = - 1;
        } else {
            $resultArrayAll ['state'] = 1;
        }
        return $resultArrayAll;
    }
    /*
     * addLevel1GetUserlimit 添加人员之获取人员数据
     */
    function addLevel1GetUserlimit($n,$m){
        $db = new SQLBase ();
        $queryStr='SELECT user_ID,user_name FROM user WHERE user_state=1 LIMIT '.$n.','.$m.';';//AND user_ID NOT IN (SELECT user_id FROM leader WHERE power_level=1)
        $result=$db->queryData($queryStr);
        if (! $result) {
            $resultArrayAll ['state'] = - 1;
        } else {
            $resultArrayAll ['state'] = 1;
            while($temp=$result->fetch_assoc()){
                $resultArrayAll ['data'][] = $temp;
            }
        }
        return $resultArrayAll;
    }
    /*
     * addLevel1GetPageCount 添加level1领导之加获取人员数量
     */
    function addLevel1GetPageCount(){
        $db = new SQLBase ();
        $queryStr='SELECT count(*) FROM user WHERE user_state=1  ;';//AND user_ID NOT IN (SELECT user_id FROM leader WHERE power_level=1)
        $result=$db->queryData($queryStr);
        if (! $result) {
            $resultArrayAll ['state'] = - 1;
        } else {
            $resultArrayAll ['state'] = 1;
            $temp=$result->fetch_assoc();
            $resultArrayAll ['pageCount'] = $temp['count(*)'];

        }
        return $resultArrayAll;
    }
    /*
     *addLevel1GetPageCount 添加level1领导之加获取人员数量
     */
    function addLevel1GetAllsectionInfo($user_id) {
        $db = new SQLBase ();
        $queryStr = 'SELECT section_ID,section_name FROM allsection WHERE section_state=1 AND section_ID NOT IN(SELECT section_id FROM leader WHERE power_level=1 AND user_id='.$user_id.'); ';//NOT IN(SELECT section_id FROM leader WHERE power_level=1)
        $result = $db->queryData ( $queryStr );
        if (! $result) {
            $resultArrayAll ['state'] = - 1;
        } else {
            $resultArrayAll ['state'] = 1;
            while ( $temp = $result->fetch_assoc () ) {
                $resultArrayAll ['data'] [] = $temp;
            }
        }
        return $resultArrayAll;
    }
    /*
     * editLevel1InsertInfo 添加LEVEL1领导
     */
    function editLevel1InsertInfo($data){
        $db = new SQLBase ();
        $queryStr = 'INSERT INTO leader(user_id,section_id,is_approve,is_fee,power_level) VALUES('.$data['userId'].','.$data['sectionId'].','.$data['isApprove'].','.$data['isFee'].',1);';
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $resultArrayAll['state']=1;
        }
        return $resultArrayAll;
    }
    /*
     * addLevel1Search 搜索要增加的领导
     */
    function addLevel1Search($earchText){
        $db = new SQLBase ();
        $queryStr='SELECT user_ID,user_name FROM user WHERE user_state=1 AND user_name LIKE "%%'.$earchText.'%%" AND user_ID NOT IN (SELECT user_id FROM leader WHERE power_level=1);';
        $result=$db->queryData($queryStr);
        if (! $result) {
            $resultArrayAll ['state'] = - 1;
        } else {
            $resultArrayAll ['state'] = 1;
            while($temp=$result->fetch_assoc()){
                $resultArrayAll ['data'][] = $temp;
            }
        }
        return $resultArrayAll;
    }
    //**********************************************************************************
    /*
     * getLevel2Limit 获取分管审批数据分页显示
     */
    function getLevel2Limit($n,$m){
        $db=new SQLBase();
        $queryStr='SELECT DISTINCT leader.user_id,`user`.user_name FROM user,leader WHERE `user`.user_ID=leader.user_id AND leader.power_level=2 LIMIT '.$n.','.$m.';';
        // 	echo $queryStr;
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $resultArrayAll['state']=1;
            $resultArrayAll['data'] = array();
            while ($temp=$result->fetch_assoc()){
                array_push($resultArrayAll['data'], $temp);
            }
        }
        return $resultArrayAll;
    }
    /*
     * editLevel2GetInfo 获取level某个人的信息
     */
    function editLevel2GetInfo($userId){
        $db=new SQLBase();
        $queryStr='SELECT section_ID,section_name FROM allsection WHERE section_state=1;';
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $resultArrayAll['state']=1;
            $resultArrayAll['allsection'] = array();
            while($temp=$result->fetch_assoc()){
                array_push($resultArrayAll['allsection'], $temp);
            }
        }
        $queryStr='SELECT section_id FROM leader WHERE is_approve=1 AND user_id='.$userId.';';
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $temp=$result->fetch_assoc();
            $resultArrayAll['state']=1;
            $resultArrayAll['isApproveData']=$temp;

        }
        $queryStr='SELECT section_id FROM leader WHERE is_fee=1 AND user_id='.$userId.';';
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $resultArrayAll['state']=1;
            $temp=$result->fetch_assoc();
            $resultArrayAll['isFeeData']=$temp;
        }
        return $resultArrayAll;
    }
    /*
     * Level2IsApproveSaveData 保存LeveL3某个人Isapprove 权限
     */
    function Level2IsApproveSaveData($userId,$sectionId){
        $db=new SQLBase();
        $queryStr='DELETE FROM leader WHERE is_approve=1 AND power_level=2 AND user_id='.$userId.';';
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            if($sectionId!=''){
                $queryStr='INSERT INTO leader(user_id,section_id,is_approve,is_fee,power_level) VALUES('.$userId.',"'.$sectionId.'",1,0,2);';
                $result=$db->queryData($queryStr);
                if(!$result){
                    $resultArrayAll['state']=-1;
                }else{
                    $resultArrayAll['state']=1;
                }
            }else{
                $resultArrayAll['state']=1;
            }
        }
        return $resultArrayAll;
    }
    /*
     * Level2IsFeeSaveData 保存LeveL3某个人IsFee 权限
     */
    function Level2IsFeeSaveData($userId,$sectionId){
        $db=new SQLBase();
        $queryStr='DELETE FROM leader WHERE is_fee=1 AND power_level=2 AND user_id='.$userId.';';
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            if($sectionId!=''){
                $queryStr='INSERT INTO leader(user_id,section_id,is_approve,is_fee,power_level) VALUES('.$userId.',"'.$sectionId.'",0,1,2);';
                $result=$db->queryData($queryStr);
                if(!$result){
                    $resultArrayAll['state']=-1;
                }else{
                    $resultArrayAll['state']=1;
                }
            }else{
                $resultArrayAll['state']=1;
            }
        }
        return $resultArrayAll;
    }
    /*
     * addLevel1GetPageCount 添加level1领导之加获取人员数量
     */
    function addLevel2GetPageCount(){
        $db = new SQLBase ();
        $queryStr='SELECT count(*) FROM user WHERE user_state=1 AND user_ID NOT IN (SELECT DISTINCT user_id FROM leader WHERE power_level=2) ;';
        // 	echo $queryStr;
        $result=$db->queryData($queryStr);
        if (! $result) {
            $resultArrayAll ['state'] = - 1;
        } else {
            $resultArrayAll ['state'] = 1;
            $temp=$result->fetch_assoc();
            $resultArrayAll ['pageCount'] = $temp['count(*)'];

        }
        return $resultArrayAll;
    }
    /*
     * addLevel2GetUserlimit 添加人员之获取人员数据
     */
    function addLevel2GetUserlimit($n,$m){
        $db = new SQLBase ();
        $queryStr='SELECT user_ID,user_name FROM user WHERE user_state=1 AND user_ID NOT IN (SELECT DISTINCT user_id FROM leader WHERE power_level=2) LIMIT '.$n.','.$m.';';
        // 	echo $queryStr;
        $result=$db->queryData($queryStr);
        if (! $result) {
            $resultArrayAll ['state'] = - 1;
        } else {
            $resultArrayAll ['state'] = 1;
            while($temp=$result->fetch_assoc()){
                $resultArrayAll ['data'][] = $temp;
            }
        }
        return $resultArrayAll;
    }
    /*
     * deleteLevel2Info 删除等级2某个领导
     */
    function deleteLevel2Info($userId) {
        $db = new SQLBase ();
        $queryStr = 'DELETE FROM leader WHERE power_level=2 AND user_id=' . $userId . ';';
        $result = $db->queryData ( $queryStr );
        if (! $result) {
            $resultArrayAll ['state'] = - 1;
        } else {
            $resultArrayAll ['state'] = 1;
        }
        return $resultArrayAll;
    }
    /*
     * addLevel2Search 搜索要增加的领导
     */
    function addLevel2Search($earchText){
        $db = new SQLBase ();
        $queryStr='SELECT user_ID,user_name FROM user WHERE user_state=1 AND user_name LIKE "%%'.$earchText.'%%" AND user_ID NOT IN (SELECT user_id FROM leader WHERE power_level=2);';
        // 	echo $queryStr;
        $result=$db->queryData($queryStr);
        if (! $result) {
            $resultArrayAll ['state'] = - 1;
        } else {
            $resultArrayAll ['state'] = 1;
            while($temp=$result->fetch_assoc()){
                $resultArrayAll ['data'][] = $temp;
            }
        }
        return $resultArrayAll;
    }
    /*
     * getLevel3Limit 获取分管审批数据分页显示
     */
    function getLevel3Limit(){
        $db=new SQLBase();
        $queryStr='SELECT DISTINCT leader.user_id,`user`.user_name,leader.is_approve,leader.is_fee FROM user,leader WHERE `user`.user_ID=leader.user_id AND leader.power_level=3;';
        // 	echo $queryStr;
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $resultArrayAll['state']=1;
            while ($temp=$result->fetch_assoc()){
                $resultArrayAll['data'][]=$temp;
            }
        }
        return $resultArrayAll;
    }
    /*
     * saveLevel3Leader 保存level3的领导信息
     */
    function saveLevel3Leader($data){
        $db=new SQLBase();
        $queryStr='DELETE FROM leader WHERE power_level=3 AND user_id='.$data['userId'].';';
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $queryStr=$queryStr='INSERT INTO leader(user_id,section_id,is_approve,is_fee,power_level) VALUES('.$data['userId'].',"all",'.$data['isApprove'].','.$data['isFee'].',3);';
            $result=$db->queryData($queryStr);
            if($result){
                $resultArrayAll['state']=1;
            }
        }
        return $resultArrayAll;
    }
    /*
     * deleteLevel3Info 删除等级2某个领导
     */
    function deleteLevel3Info($userId) {
        $db = new SQLBase ();
        $queryStr = 'DELETE FROM leader WHERE power_level=3 AND user_id=' . $userId . ';';
        $result = $db->queryData ( $queryStr );
        if (! $result) {
            $resultArrayAll ['state'] = - 1;
        } else {
            $resultArrayAll ['state'] = 1;
        }
        return $resultArrayAll;
    }
    /*
     * addLevel1GetPageCount 添加level1领导之加获取人员数量
     */
    function addLevel3GetPageCount(){
        $db = new SQLBase ();
        $queryStr='SELECT count(*) FROM user WHERE user_state=1 AND user_ID NOT IN (SELECT DISTINCT user_id FROM leader WHERE power_level=3) ;';
        // 	echo $queryStr;
        $result=$db->queryData($queryStr);
        if (! $result) {
            $resultArrayAll ['state'] = - 1;
        } else {
            $resultArrayAll ['state'] = 1;
            $temp=$result->fetch_assoc();
            $resultArrayAll ['pageCount'] = $temp['count(*)'];

        }
        return $resultArrayAll;
    }
    /*
     * addLevel2GetUserlimit 添加人员之获取人员数据
     */
    function addLevel3GetUserlimit($n,$m){
        $db = new SQLBase ();
        $queryStr='SELECT user_ID,user_name FROM user WHERE user_state=1 AND user_ID NOT IN (SELECT DISTINCT user_id FROM leader WHERE power_level=3) LIMIT '.$n.','.$m.';';
        // 	echo $queryStr;
        $result=$db->queryData($queryStr);
        if (! $result) {
            $resultArrayAll ['state'] = - 1;
        } else {
            $resultArrayAll ['state'] = 1;
            while($temp=$result->fetch_assoc()){
                $resultArrayAll ['data'][] = $temp;
            }
        }
        return $resultArrayAll;
    }
    /*
     * addLevel3Search 搜索要增加的领导
     */
    function addLevel3Search($earchText){
        $db = new SQLBase ();
        $queryStr='SELECT user_ID,user_name FROM user WHERE user_state=1 AND user_name LIKE "%%'.$earchText.'%%" AND user_ID NOT IN (SELECT user_id FROM leader WHERE power_level=3);';
        // 	echo $queryStr;
        $result=$db->queryData($queryStr);
        if (! $result) {
            $resultArrayAll ['state'] = - 1;
        } else {
            $resultArrayAll ['state'] = 1;
            while($temp=$result->fetch_assoc()){
                $resultArrayAll ['data'][] = $temp;
            }
        }
        return $resultArrayAll;
    }
}